package ihm;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import database.ConnexionDB;
import data.*;

import java.awt.Button;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JCheckBox;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComboBox;
public class FenetreModifierIndice extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private ModeleDynamiqueObjetIndice modele;
	private String Type_fichier;
	
	private ConnexionDB DB;
	
	//private ModeleDynamiqueObjetIndice  modele = new ModeleDynamiqueObjetIndice ();
    private JTable tableau;
    private JTextField txtDsq;
    private JTextField textField_2;
    private JTextField textField_3;
   // private final Action action = new SwingAction();



	/**
	 * Create the frame.
	 */
	public FenetreModifierIndice(ModeleDynamiqueObjetIndice mdoui,final int id_indice, final Authentification auth,final Log log) throws SQLException {
		setTitle("Murder Party - Modifier un indice");		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
		
		modele = mdoui;
		
		final Indice ind1 = modele.getInfosIndice(id_indice);
		
	System.out.println(ind1.getCoutIndice());
		
		
	
		ArrayList<Utilisateur> listUser = new ArrayList<Utilisateur>();
		
		
			listUser = getJoueurIndice(id_indice);
		
		

		setBounds(100, 100, 407, 460);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField(ind1.getLabelIndice());
		textField.setBounds(108, 11, 180, 35);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Chemin");
		lblNewLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
		lblNewLabel.setBounds(24, 64, 66, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Type :");
		lblNewLabel_1.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
		lblNewLabel_1.setBounds(24, 101, 46, 14);
		contentPane.add(lblNewLabel_1);

	
		
		
		
		
		
		
		JLabel lblJoueurPossdantLindice = new JLabel("Joueur poss\u00E9dant l'indice");
		lblJoueurPossdantLindice.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		lblJoueurPossdantLindice.setBounds(217, 188, 174, 14);
		contentPane.add(lblJoueurPossdantLindice);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
		lblDescription.setBounds(24, 188, 110, 14);
		contentPane.add(lblDescription);
		
		final JTextArea textArea = new JTextArea(ind1.getDescription_indice());
		textArea.setBounds(24, 209, 187, 155);
		contentPane.add(textArea);
		
		JButton btnValider = new JButton("Valider");
		btnValider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				
				
				if ( textField.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null,"Veuillez choisir un nom");
			
				}
				else if (textField_3.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null,"Veuillez choisir un co�t");
				
				}
				else if (textArea.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null,"Veuillez �crire une d�scription");
				
				}
			
				else
				{	
				Indice ind2 = new Indice();
				
			
				ind2.setIdIndice(id_indice) ;
				ind2.setLabelIndice(textField.getText()) ;
				ind2.setCheminIndice(txtDsq.getText()) ;
				ind2.setDescription_indice(textArea.getText()) ;
				ind2.setTypeIndice(textField_2.getText());
				int temp = Integer.parseInt(textField_3.getText());
				ind2.setCoutIndice( temp);
				
			if (!ind1.getCheminIndice().equals(ind2.getCheminIndice()))
			{
		
				String source = "data\\"+ind1.getCheminIndice() ;
		        String dir = "data\\"+ind2.getCheminIndice() ;

	        	  File f1 = new File(source);
	        	  File f2 = new File(dir);
	          	    
	        	  try{
	        		  
	        	  
	        	  InputStream in = new FileInputStream(f1);
	        	  OutputStream out = new FileOutputStream(f2);
	        	  byte[] buf = new byte[1024];
	        	  int len;
	        	  while ((len = in.read(buf)) > 0){
	        	  out.write(buf, 0, len);
	        	  }
	        	  in.close();
	        	  out.close();
	        	  System.out.println("File copied.");
	        	  }
	        	  catch(Exception e) {
			
			e.printStackTrace();
		}
	        	  
			}
	       

	        	  
	        	  
	        	  
				
				modele.modifier_Indice(ind2);
							
			
				log.addLine("[+] Modification de l'indice:"+ind2.getLabelIndice());
				FermerFenetre();
		
		}
			}
			});
		btnValider.setBounds(103, 388, 89, 23);
		contentPane.add(btnValider);
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {	
			
			FermerFenetre();
		
	}

		

		
		});
	
		txtDsq = new JTextField(ind1.getCheminIndice());
		txtDsq.setEditable(false);
		txtDsq.setBounds(83, 63, 205, 20);
		contentPane.add(txtDsq);
		txtDsq.setColumns(10);
		
		
		btnAnnuler.setBounds(225, 388, 89, 23);
		contentPane.add(btnAnnuler);
		
		final JButton btnNewButton = new JButton("Voir l'indice");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				System.out.println(txtDsq.getText());
				if ( !(txtDsq.getText().equals("")) )
				{
				
				File fichier_indice = new File("data\\"+txtDsq.getText() );
				boolean test =OuvrirFile(fichier_indice);
				if ( test == true )
				{
					System.out.println("test = true");
				}
				else
				{
					System.out.println("test = false");
				}
			}
				
			}
		});
		btnNewButton.setBounds(253, 136, 110, 23);
		contentPane.add(btnNewButton);
		
		
		
		textField_2 = new JTextField(ind1.getTypeIndice());
		textField_2.setEditable(false);
		textField_2.setBounds(80, 100, 138, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		final JButton btnChanger = new JButton("Changer...");
		btnChanger.setBounds(298, 62, 89, 23);
		contentPane.add(btnChanger);
		
		btnChanger.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
		
				try {
					JFileChooser chooser = new JFileChooser();
					chooser.setBounds(0, 0, 200, 202);
					contentPane.add(chooser);
				    int answer = chooser.showOpenDialog(btnChanger);  
				    if (answer == JFileChooser.APPROVE_OPTION)  
				    	{  
				    	File file = chooser.getSelectedFile();  
				    	 String dir  = "indices\\"+file.getName();
				    	txtDsq.setText(dir);
				    	
				    	Type_fichier = chooser.getTypeDescription(file);  
				    	textField_2.setText(Type_fichier);
				    	btnNewButton.setVisible(false);
				    	}  
				    
			
				} catch (Exception e) {
					
					e.printStackTrace();
				}
			}

		}
		);
	
		
		
		
		
		final JComboBox comboBox = new JComboBox();
		int i;
		for ( i = 0 ; i < listUser.size();i++){
			

		
		comboBox.addItem(getPseudoUser(listUser.get(i).getIdUtilisateur()));
			
		}
	
		
	
		comboBox.setBounds(225, 227 , 138, 20);
		contentPane.add(comboBox);
		
		JButton btnEnleverLindice = new JButton("Enlever l'indice");
		btnEnleverLindice.setBounds(242, 331, 126, 23);
		btnEnleverLindice.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
		
			int id_joueur = modele.getIdJoueur((String)comboBox.getSelectedItem()) ;
			
			
			
			enlever_indice(id_indice,id_joueur);

			ArrayList<Utilisateur> listUser2 = new ArrayList<Utilisateur>();			
			listUser2 = getJoueurIndice(id_indice);
			int i;
			System.out.println("taille = "+listUser2.size());
			
			comboBox.removeAllItems();
			
		
			if ( listUser2.size() != 0 )
			{
			for ( i = 0 ; i < listUser2.size();i++){
			
			
				comboBox.addItem(getPseudoUser(listUser2.get(i).getIdUtilisateur()));
					
				}
			
			}
			
			// mise a jour client
			auth.setUtilisateur(auth.getUtilisateur());
			
			}
		});
		contentPane.add(btnEnleverLindice);
		
		JLabel lblCout = new JLabel("Cout : ");
		lblCout.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
		lblCout.setBounds(24, 152, 66, 14);
		contentPane.add(lblCout);
		
		textField_3 = new JTextField(String.valueOf(ind1.getCoutIndice()));
		textField_3.setBounds(69, 151, 28, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblMins = new JLabel("mins");
		lblMins.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
		lblMins.setBounds(108, 152, 46, 14);
		contentPane.add(lblMins);
	}



	protected void enlever_indice(int id_indice, int id_joueur) {
		
		System.out.println("test1");
		
		
		modele.enleverIndiceJoueur(id_indice,id_joueur);
	}



	protected static void closeFenetre() {
	System.exit(0);
	}



	private String getPseudoUser(int idUtilisateur) {

		
		
	return modele.getPseudoUser(idUtilisateur);

}



	private  ArrayList<Utilisateur> getJoueurIndice(int id_indice) {
		
		return modele.getJoueurIndice(id_indice);
		
	}
	private void FermerFenetre() {
	this.dispose();
		
	}
	
	


public boolean OuvrirFile(final File file) {
	  if (!Desktop.isDesktopSupported()) {
	    return false;
	  }

	  Desktop desktop = Desktop.getDesktop();
	  if (!desktop.isSupported(Desktop.Action.OPEN)) {
	    return false;
	  }

	  try {
	    desktop.open(file);
	  } catch (IOException e) {
	    // Log an error
	    return false;
	  }

	  return true;
	}

}

